package main

import (
	"fmt"
	"sort"
)

func main() {
	/*
		案例2:对map对象的key进行升序，然后输出一个key=>value
	*/
	var m1 = map[string]string{
		"username": "zhangsan",
		"age":      "20",
		"sex":      "男",
		"height":   "180",
		"salt":     "xxxx",
	}
	fmt.Println(mapToString(m1))
}

func mapToString(m1 map[string]string) string {
	var str string
	var keys []string

	//提取map键
	for k, _ := range m1 {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, key := range keys {
		str += fmt.Sprintf("%v=>%v", key, m1[key])
	}

	return str
}
